#include "dictationdialog.h"
#include "ui_dictationdialog.h"
#include <QPushButton>
#include <QTimer>

namespace WordMemory {

    DictationDialog::DictationDialog(QWidget *parent)
        : QDialog(parent)
        , ui(new Ui::DictationDialog)
    {
        ui->setupUi(this);
        setupCustomButtons();
        setModal(true);
        connect(ui->wordInputEdit, &QLineEdit::returnPressed, this, &DictationDialog::onConfirmClicked);
    }

    DictationDialog::~DictationDialog()
    {
        delete ui;
    }

    void DictationDialog::setupCustomButtons()
    {
        confirmButton = ui->buttonBox->addButton("确认", QDialogButtonBox::AcceptRole);
        abandonButton = ui->buttonBox->addButton("放弃", QDialogButtonBox::DestructiveRole);
        finishButton  = ui->buttonBox->addButton("结束", QDialogButtonBox::RejectRole);

        confirmButton->setStyleSheet("background-color: #52c41a; color: white;"); // Green
        abandonButton->setStyleSheet("background-color: #faad14; color: white;"); // Orange
        finishButton->setStyleSheet("background-color: #ff4d4f; color: white;");  // Red

        connect(confirmButton, &QPushButton::clicked, this, &DictationDialog::onConfirmClicked);
        connect(abandonButton, &QPushButton::clicked, this, &DictationDialog::onAbandonClicked);
        connect(finishButton, &QPushButton::clicked, this, &DictationDialog::onFinishClicked);
    }

    void DictationDialog::startDictation(const Word& wordToDictate)
    {
        currentWord = wordToDictate;
        ui->translationLabel->setText(currentWord.meaning);
        ui->wordInputEdit->clear();
        ui->wordInputEdit->setFocus();
        clearFeedback();
    }

    QString DictationDialog::getEnteredWord() const
    {
        return ui->wordInputEdit->text().trimmed();
    }

    Word DictationDialog::getCurrentWord() const
    {
        return currentWord;
    }

    void DictationDialog::clearFeedback()
    {
        ui->feedbackLabel->clear();
        ui->feedbackLabel->setStyleSheet("");
    }

    void DictationDialog::onConfirmClicked()
    {
        if (currentWord.id == 0) return;

        QString enteredText = getEnteredWord();
        if (enteredText.isEmpty()) {
            ui->feedbackLabel->setText("请输入单词!");
            ui->feedbackLabel->setStyleSheet("color: orange;");
            return;
        }

        bool isCorrect = (enteredText.toLower() == currentWord.word.toLower());

        if (isCorrect) {
            ui->feedbackLabel->setText("正确!");
            ui->feedbackLabel->setStyleSheet("color: green;");
            emit dictationActionTaken(DictationResult::ConfirmedCorrect, currentWord);
        } else {
            ui->feedbackLabel->setText("错误! 正确答案: " + currentWord.word);
            ui->feedbackLabel->setStyleSheet("color: red;");
            emit dictationActionTaken(DictationResult::ConfirmedWrong, currentWord);
        }
    }

    void DictationDialog::onAbandonClicked()
    {
        if (currentWord.id == 0) return;
        emit dictationActionTaken(DictationResult::Abandoned, currentWord);
    }

    void DictationDialog::onFinishClicked()
    {
        emit dictationActionTaken(DictationResult::Finished, currentWord);
        QDialog::reject();
    }

  }
